package training.project.app.web;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import training.project.app.domain.FlightClient;
import training.project.app.domain.UserAccount;
import training.project.app.service.FlightClientService;
import training.project.app.service.FlightService;
import training.project.app.service.UserService;

@Controller
@RequestMapping("/profile")
public class FlightController
	{
		@Autowired
		private FlightService flightService;
		@Autowired
		private FlightClientService flightClientService;
		@Autowired
		private UserService clientService;

		@PreAuthorize("isFullyAuthenticated() and hasAnyRole('ROLE_ADMIN','ROLE_USER')")
		@RequestMapping(value = "/flights", method = RequestMethod.GET)
		public ModelAndView showFlights(HttpSession session, Model model)
			{
				ModelAndView mav = new ModelAndView();
				mav.setViewName("flightList");
				return mav;
			}

		@PreAuthorize("isFullyAuthenticated() and hasAnyRole('ROLE_ADMIN','ROLE_USER')")
		@RequestMapping(value = "/flights/my", method = RequestMethod.GET)
		public ModelAndView showMyFlights(HttpSession session, Model model)
			{
				ModelAndView mav = new ModelAndView();
				Object userObject = session.getAttribute("user");
				if ((userObject != null) && (userObject instanceof UserAccount))
					{
						UserAccount user = (UserAccount) userObject;
						System.out.println(user);
						mav.addObject("flights", flightClientService
								.getFlightClients(user.getUserId()));
						mav.setViewName("UserFlight");
					}
				return mav;
			}

		/*
		 * @PreAuthorize(
		 * "isFullyAuthenticated() and hasAnyRole('ROLE_ADMIN','ROLE_USER')")
		 * 
		 * @RequestMapping(value = "/flights/{id}/book", method =
		 * RequestMethod.GET) public ModelAndView BookFlight(HttpSession
		 * session, Model model) {
		 * 
		 * 
		 * }
		 */
		@RequestMapping(value = "/flights/{id}/purchase", method = RequestMethod.GET)
		public ModelAndView purchaseFlight(@PathVariable String id,
				@PathVariable("id") FlightClient flightClient,
				HttpSession session, Model model)
			{
				ModelAndView mav = new ModelAndView();
				mav.setViewName("UserFlight");
				if (clientService.purchaseFlight(flightClient.getUser(),
						flightClient))
					{
						flightClientService.updateFlightClient(flightClient);
						clientService.updateClient(flightClient.getUser());
						flightService.updateFlight(flightClient.getFlight());
					}
				else
					{
						mav.addObject("error", "You don't have enought money");
					}
				mav.addObject("flights",
						clientService.getFlightClients(flightClient.getUser()));
				return mav;
			}

		@RequestMapping(value = "/flights/{id}/cancel", method = RequestMethod.GET)
		public ModelAndView cancelFlight(@PathVariable Integer id,
				HttpSession session, Model model)
			{
				ModelAndView mav = new ModelAndView();
				flightClientService.cancelFlight(flightClientService
						.getFlightClientById(id));
				UserAccount user = (UserAccount) session.getAttribute("user");
				mav.addObject("flights", clientService.getFlightClients(user));
				mav.setViewName("UserFlight");
				return mav;
			}
	}